@tool # 加上 @tool 以便在预览的时候显示正确的角色皮肤而非默认皮肤
extends CharacterBody2D

class_name Human

@export var Appearance: SpriteFrames

@onready var anim = $Anim
@onready var controller = $ActionController as ActionController
@onready var pb = $ProgressBar as ProgressLine # 进度条
@onready var navi = $NavigationAgent2D as NavigationAgent2D

var direction: Vector2 = Vector2(0, 0) 
var speed: = 100.0    
var chair: Chair
var food: Food

# Called when the node enters the scene tree for the first time.
func _ready():
	anim.sprite_frames = Appearance
	
func setX(x):
	direction.x	= x

func setY(y):
	direction.y = y

func _process(delta):
	move()

func naviTo(pos):
	navi.target_position = pos

func _physics_process(delta):
	if not Engine.is_editor_hint():
		if navi.is_navigation_finished():
			move_and_collide(velocity*delta)
			return
		var moveDir = position.direction_to(navi.target_position)
		velocity = moveDir.normalized() * speed / 60
		move_and_collide(velocity)

func setRun():
	speed = 150
	
func setWalk():
	speed = 100	

## 移动	
func move():
	if pb.isProgressing: 
		return
	var vecMove = direction.normalized() # normalized 一下，让向量长度 = 1，保持匀速移动
	
	if vecMove.y > 0: 
		anim.play("down")
	elif vecMove.y < 0: 
		anim.play("up")
	elif vecMove.x < 0:
		anim.play("left")
	elif vecMove.x > 0:
		anim.play("right")
	velocity = vecMove * speed
	
func startProgress():
	velocity = Vector2.ZERO
	pb.start()

func _on_progress_bar_done(value):
	print("progress done.")

func _on_hand_right(body):
	print("handle_right", body)
	if body is Food:
		print("is food")

func _on_area_2d_body_entered(body):
	print("hand enter", body, body.name)


func _on_navigation_agent_2d_target_reached():
	pass # Replace with function body.
